﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IngredSubTool
{
    /// <summary>
    /// Interaction logic for BookmarkView.xaml
    /// </summary>
    public partial class BookmarkView : UserControl, IAppPage
    {

        public List<Substitution> bookSubs = new List<Substitution>();
        public BookmarkView()
        {
            InitializeComponent();
            Menu.Content = new SideMenu();
            BookmarkGrid.ItemsSource = MainWindow.session.bookmarkSubs;
           

        }

        private void Back(object sender, RoutedEventArgs e)
        {
            PageSwitcher.SwitchBack();
        }

        public void BookmarkAdd(Substitution subObj)
        {
            bookSubs.Add(subObj);
        }

        private void RatiosBtn_Click(object sender, RoutedEventArgs e)
        {
            Substitution subtemp = (Substitution)BookmarkGrid.CurrentItem;
            MainWindow.session.selectedSub = subtemp;
            PageSwitcher.SwitchForward(new ConversionCalcView());
        }

        private void CommentSend_Click(object sender, RoutedEventArgs e)
        {
            //Get parent substitution for this comment by climbing the visual tree.
            Button btn = sender as Button;
            var parent = VisualTreeHelper.GetParent(btn);
            while (parent != null && parent.GetType() != typeof(DataGridRow))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            DataGridRow row = parent as DataGridRow;
            Substitution sub = row.Item as Substitution;

            //Get entered info.
            StackPanel stack = VisualTreeHelper.GetParent(btn) as StackPanel;
            TextBox userElem = stack.FindName("Username") as TextBox;
            TextBox commElem = stack.FindName("Comment") as TextBox;
            ComboBox ratiElem = stack.FindName("Rating") as ComboBox;

            String username = userElem.Text;
            String comment = commElem.Text;
            int rating = ratiElem.SelectedIndex;

            userElem.Text = "";
            commElem.Text = "";
            ratiElem.SelectedIndex = 0;

            //Create comment.
            Comment newComment = new Comment(sub.Original, sub.Replacement, username, comment, rating, DateTime.Now);

            //Add comment to substitution.
            sub.Comments.Add(newComment);
            sub.UpdateAverage();

            btn.Foreground = Brushes.Red;
            btn.Content = "Comment Sent";

            //Ensure scrollpanel is at bottom.
            parent = VisualTreeHelper.GetParent(btn);
            while (parent != null && parent.GetType() != typeof(ScrollViewer))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            ScrollViewer scroll = parent as ScrollViewer;
            scroll.ScrollToEnd();
        }

        private void Button_LostFocus(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            btn.Foreground = Brushes.Black;
            btn.Content = "Send Comment";
        }
    }
}
